class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int left = 0;
        int right = nums.size() - 1;
        int end = right - 1;
        int count = 0;
        while (right >= 2)
        {
            left = 0;
            end = right - 1;
            while (left < end)
            {
                if (nums[left] + nums[end] > nums[right])
                {
                    count += end - left;
                    end--;
                }
                else
                {
                    left++;
                }
            }
            right--;
        }
        return count;
    }
};